const db = wx.cloud.database()
const app = getApp();
const user=db.collection('user')
Page({
    /*** 页面的初始数据*/
    data: {
        textareaTxt:null,
        imgArr:null,
    },
    saveEditOrNot(){  //取消按钮
        
    },
    getInputValue(e){ //输入框
        console.log(e)
        this.setData({
            textareaTxt: e.detail.value    
        })
    },
    async chooseImage(){   //选择图片
        let self = this;
        let res = await wx.chooseImage({
            count: 9
        })
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        let filenameArr = new Array//新建一个数组存放文件名
        let purfilenameArr = new Array
        res.tempFilePaths.forEach(v=>{
            filenameArr.push(v)
            purfilenameArr.push(v.split('/').slice(-1)[0])
        })
        console.log(purfilenameArr)
        console.log(filenameArr)
        self.setData({
            imgArr: res.tempFilePaths,
            filenameArr:filenameArr,
            purfilenameArr:purfilenameArr
        })
    },
    async postData(){  //发表按钮
        let check=this.checkLogin()
        if(!check){
            return
        }
        wx.showLoading({
          title: '发表中',
          mask:true
        })
        let textareaTxt = this.data.textareaTxt
        let filenameArr = this.data.filenameArr
        let purfilenameArr = this.data.purfilenameArr
        let fileidArr = new Array
        if(filenameArr&&textareaTxt){
        for(let i=0;i<filenameArr.length;i++){
            var res = await wx.cloud.uploadFile({
                cloudPath:'comment/'+purfilenameArr[i],  //上传路径
                filePath:filenameArr[i]   //本地路径
              })
            fileidArr.push(res.fileID) //回传文件id
        }
        res = await wx.cloud.callFunction({
            name:'addFoodComment',
            data:{
                fileidArr:fileidArr,
                textareaTxt:this.data.textareaTxt,
                openid:app.globalData.openid,
                nickName:app.globalData.nickName,
                avatarUrl:app.globalData.avatarUrl
            }
        })
        console.log('addFoodComment:',res)
        let _id = res.result.Msg._id
        if(res.errMsg.indexOf('ok')>-1){
            wx.showToast({  //toast 提示
              title: '发表成功',
              duration:1000,   
              success: function () {  //提示成功1000毫秒后再重新跳转
                setTimeout(function() {
                  wx.redirectTo({
                    url: '/pages/comment/incomment/incomment?_id='+_id,
                  })
                }, 1000);
              }
            })
          }
       }else{
            wx.showToast({
              title: '图片或文字不能为空',
              icon:'none'
            })
       }
       console.log(app)
       wx.hideLoading()
    },
    checkLogin(){//检查登陆
        let openid=app.globalData.openid
        let that=this
        if(!openid){
            console.log('未登陆')
            wx.showModal({
                title:'你还未登陆',
                content:'请先登陆',
                cancelText:'取消',
                confirmText:'登陆',
                success(res){
                    if(res.confirm){
                        console.log('点了确定')
                        that.login()
                    }
                }
            })
            return false
        }else{
            console.log('已登陆')
            return true
        }
    },
    async login(){//登陆
        console.log(app.globalData.nickName)
        let _id=this.data._id
        let res=await wx.getUserProfile({
          desc: 'desc',
        })
        console.log('res:',res)
        let {avatarUrl,nickName}=res.userInfo      
        res=await wx.cloud.callFunction({
          name:'login',
          data:{
            avatarUrl,nickName
          }
        })
        console.log('登陆结果',res)
        if(res.result.nickName){
          wx.showToast({
            title: '登陆成功',
          })
          let openid= await user.where({
            nickName:nickName,
            avatarUrl:avatarUrl
          })
          .get()
          openid=openid.data[0].openid
          console.log('openid',openid)
          //写入用户数据到客户端↓↓↓
          this.setData({
            avatarUrl:res.result.avatarUrl,
            nickName:res.result.nickName,
            openid
          })
          wx.setStorage({
            key:'user',
            data:{'avatarUrl':avatarUrl,'nickName':nickName,'openid':openid}
          })
          app.globalData.avatarUrl=avatarUrl
          app.globalData.nickName=nickName
          app.globalData.openid=openid
          console.log(app.globalData.nickName)
          console.log(app.globalData.openid)
        }else{
          wx.showToast({
            title: '登陆失败',
            icon:'error'
          })
        }
      },
})